<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
	<head>
		<meta charset="utf-8">
		<title>用户管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
	<!--查询表单-->
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">用户名</label>
							<div class="layui-input-inline">
								<input type="text" name="uname" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">性别</label>
							<div class="layui-input-inline">
								<input type="text" name="usex" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">电话</label>
							<div class="layui-input-inline">
								<input type="text" name="tel" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">账号</label>
							<div class="layui-input-inline">
								<input type="text" name="account" placeholder="" class="layui-input">
							</div>
						</div>
<!--						<div class="layui-form-item layui-inline">-->
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
<!--						</div>-->
					</div>
				</form>
			</div>
		</div>
	<!--信息展示表格-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="user-table" lay-filter="user-table"></table>
			</div>
		</div>
		<!--添加、修改   组件-->
		<div style="display: none" id="showAdd">
			<form class="layui-form" action="" lay-filter="addForm">
				<input type="hidden" name="uid">
				<div class="mainBox">
					<div class="main-container">
						<div class="main-container">
							<div class="layui-form-item">
								<label class="layui-form-label">账号</label>
								<div class="layui-input-block">
									<input type="text" name="account" lay-verify="title" autocomplete="off" placeholder="请输入账号" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">姓名</label>
								<div class="layui-input-block">
									<input type="text"  name="uname" lay-verify="title" autocomplete="off" placeholder="请输入姓名" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">邮箱</label>
								<div class="layui-input-block">
									<input type="text"  name="email" lay-verify="title" autocomplete="off" placeholder="请输入email" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">密码</label>
								<div class="layui-input-block">
									<input type="text"  name="upwd" lay-verify="title" autocomplete="off" placeholder="请输入密码" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">电话</label>
								<div class="layui-input-block">
									<input type="text"  name="tel" lay-verify="title" autocomplete="off" placeholder="请输入电话" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">性别</label>
								<div class="layui-input-block">
									<input type="radio" name="usex" value="1" title="男" checked>
									<input type="radio" name="usex" value="0" title="女">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">权限</label>
								<div class="layui-input-block">
									<select id="right" name="aid">
										<option value="1"> 超级管理员</option>
										<option value="2"> 普通管理员</option>
										<option value="3"> 普通用户</option>
									</select>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="bottom">
					<div class="button-container">
						<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-submit">
							<i class="layui-icon layui-icon-ok"></i>
							提交
						</button>
						<button type="reset" class="pear-btn pear-btn-sm">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</div>
			</form>
		</div>

		<!--新增 ，批量删除按钮组件-->
		<script type="text/html" id="user-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
		</script>
		<!--修改，删除按钮-->
		<script type="text/html" id="user-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
		<!--启用禁用组件-->
		<script type="text/html" id="user-enable">
			<input type="checkbox" id="user-checkbox" name="enable" value="{{d.uid}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{d.enable==1?'checked':''}}>
		</script>
		<!--时间格式化组件-->
		<script type="text/html" id="user-createTime">
			{{layui.util.toDateString(d.createtime, 'yyyy-MM-dd')}}
		</script>
		<script type="text/html" id="user-back">
			{{d.enable===1?"启用":"禁用"}}
		</script>
		<script type="text/html" id="user-sex">
			{{d.usex==1?"男":"女"}}
		</script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "operate/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '序号',
							field: 'uid',
							align: 'center',
							width: 100
						},
						{
							title: '账号',
							field: 'account',
							align: 'center',
							width: 100
						},
						{
							title: '姓名',
							field: 'uname',
							align: 'center'
						},
						{
							title: '性别',
							field: 'usex',
							align: 'center',
							width: 80,
							templet: '#user-sex'
						},
						{
							title: '电话',
							field: 'tel',
							align: 'center'
						},
						{
							title: '状态',
							field: 'enable',
							align: 'center',
							templet: '#user-enable'
						},

						{
							title: '注册日期',
							field: 'createtime',
							align: 'center',
							templet: '#user-createTime',
							sort: true
						},
                        {
                            title: '备注',
                            field: 'back',
                            align: 'center',
                           templet: '#user-back'
                        },

						{
							title: '操作',
							toolbar: '#user-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#user-table',//指定原始表格元素选择器（推荐id选择器）
					url: 'http://localhost:8080/users/select',
					page: true,
					cols: cols,//设置表头
					skin: 'line',//表格风格
					toolbar: '#user-toolbar',//组件栏
					defaultToolbar: [//右上角组件栏
							{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					},
						'filter',
						'print',
						'exports'
					]
				});

				table.on('tool(user-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(user-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(user-query)', function(data) {
					table.reload("user-table",
							{
								where:data.field,
								page: {curr: 1}
							})
					return false;
				});

				form.on('switch(user-enable)', function(obj) {
					layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
					let enable = obj.elem.checked?1:0;
					let loading = layer.load(2);
					$.post(
							"http://localhost:8080/users/updateOne",
							{uid:this.value,enable:enable},
							function (msg){
								table.reload("user-table")
								layer.close(loading)
								layer.msg("操作成功",{icon: 6,time: 2000})
							}
					)
					return false;
				});

				let index = 0;
				window.add = function() {
					index = layer.open({
						type: 1,
						title: '新增用户',
						area: ['500px', '600px'],
						content: $('#showAdd')
					});
					//表单提交
					form.on('submit(user-submit)', function(data){
						let loading = layer.load(2);
						$.post(
								"http://localhost:8080/users/addOne",
								data.field,
								function (msg){
									layer.close(index)
									layer.close(loading)
									table.reload("user-table")
									layer.msg('添加成功',{icon: 6,time: 2000})
								}
						)
						return false;
					});
				}

				//修改
				window.edit = function(obj) {
					let loading = layer.load(2);
					$.get(
							"http://localhost:8080/users/selectOne",
							{id:obj.data.uid},
							function (data){
								//打开
								index = layer.open({
									type: 1,
									title: '修改用户',
									area: ['500px', '600px'],
									content: $('#showAdd') //注意，如果str是object，那么需要字符拼接。
								});
								form.val("addForm",data)
								layer.close(loading)
							},
					)
					form.on('submit(user-submit)', function(data){
						let loading = layer.load(2);
						//提交数据
						$.post(
								'http://localhost:8080/users/updateOne',
								data.field,
								function(msg){
									layer.close(index);//关闭当前页
									table.reload("user-table");
									layer.close(loading)
									layer.msg('修改成功',{icon: 6,time: 2000})
								}
						)
						//阻止表单提交
						return false;
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						let loading = layer.load(2);
						layer.close(index);
						console.log({uid:obj.data.uid})
						$.get(
								"http://localhost:8080/users/deleteOne",
								{uid:obj.data.uid},
								function (msg){
									table.reload("user-table")
									layer.close(loading)
									layer.msg("删除成功",{icon:6,time:2000})
								}
						);

					});
				}

				window.batchRemove = function(obj) {
					let data = table.checkStatus(obj.config.id).data;
					console.log(obj.config)
					console.log(data)
					console.log(obj.config.id)
					if (data.length == 0) {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let ids = "";
					for (let i = 0;i<data.length;i++){
						ids+=data[i].uid+","
					};
					ids=ids.substr(0,ids.length-1)
					layer.confirm('确定要删除这些用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load(2);
						$.get(
								"http://localhost:8080/users/batchRemove",
								{uids:ids},
								function (msg){
									table.reload("user-table");
									layer.close(loading)
									layer.msg("删除成功",{icon:6,time:2000})
								}
						)
					});
				}

				window.refresh = function(param) {
					table.reload('user-table');
				}
			})
		</script>
	</body>
</html>
